<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hadd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" style="width: 100%">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="hAssign(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hdel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2, 5, 10, 20]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @current-change="hcurrent"
                @size-change="hsize"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        title="编辑弹层"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hclose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹层 -->
      <el-dialog :visible.sync="showroRole">
        <assignPer v-if="showroRole" :roleid="roleId" @close="showroRole=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import assignPer from './assignPermission.vue'
import { getRoles, addRoles, delRoles, getRolesDetail, updateRoles } from '@/api/settings'
export default {
  components: {
    assignPer
  },
  data() {
    return {
      roleId: '', // 分配权限时角色的id
      showroRole: false, // 控制分配权限
      isEdit: false, // false为新增状态,true为编辑状态
      list: [], // 角色列表数据
      pageParams: {
        page: 1, // 页码值
        pagesize: 2 // 每页显示的条数
      },
      total: 0, // 角色的总数
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 分配权限弹层开启
    hAssign(id) {
      // 记下来id
      this.roleId = id
      this.showroRole = true
    },
    // 关闭弹层,重置表单
    hclose() {
      this.$refs.roleForm.resetFields()
    },
    // 弹层确认框
    hSubmit() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        this.isEdit ? this.loadupdateRoles() : this.loadaddRoles()
      })
    },
    // 新增角色功能
    async loadaddRoles() {
      try {
        const res = await addRoles(this.roleForm)
        console.log(res, '新增的角色')
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 获取最大页码值,获得手动获得更新后的状态
        this.total++
        const maxpage = Math.ceil(this.total / this.pageParams.pagesize)
        this.pageParams.page = maxpage
        this.loadRoles()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 删除角色功能
    hdel(id) {
      this.$confirm('你确定要删除吗').then(async() => {
        const res = await delRoles(id)
        console.log(res, '删除的数据返回值')
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        if (this.list.length === 1) this.pageParams.page--
        if (this.pageParams.page <= 0) this.pageParams.page = 1
        // 重新请求
        this.loadRoles()
        this.showDialog = false
      }).catch((error) => {
        console.log(error)
      })
    },
    // 编辑角色功能
    async loadupdateRoles() {
      try {
        const res = await updateRoles(this.roleForm)
        console.log(res, '编辑的角色')
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadRoles()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑状态
    hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      this.loadRolesDetail(id)
    },
    // 添加状态
    hadd() {
      this.isEdit = false
      this.showDialog = true
    },
    // 编辑状态下填充表单
    async loadRolesDetail(id) {
      try {
        const res = await getRolesDetail(id)
        console.log('编辑状态下,获取的某个角色数据', res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.roleForm = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 获取当前页角色信息
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.total = res.data.total
        this.list = res.data.rows
        console.log('查询该页面下角色列表', res)
      } catch (error) {
        console.log(error)
      }
    },
    // 页码值改变时触发,更新
    hcurrent(val) {
      this.pageParams.page = val
      this.loadRoles()
    },
    // 每页显示的条数改变,更新当前角色列表
    hsize(val) {
      this.pageParams.pagesize = val
      this.loadRoles()
    }
  }
}
</script>
